845B - Luba And The Ticket - CodeForces Solution


brute force greedy implementation *1600

Please click on ads to support us..

Python Code:

def ss(s):
    a=[]
    b=[]
    for i in s[:3]:
        a.append(int(i))
    for i in s[3:]:
        b.append(int(i))

    x=sum(a)
    y=sum(b)
    if x==y:
        return 0
    for i in a:
                if y>=x-i and x-i+9>=y:
            return 1 
    for i in b:
        if x>=y-i and y-i+9>=x:
            return 1 
    
    for i in a:
        for j in b:
                                                    
            if x>y:
                if y-j+9>=x-i:
                    return 2
            else:
                if x-i+9>=y-j:
                    return 2
    
    for i in range(3):
        for j in range(i+1,3):
            if y>=x-a[i]-a[j] and x-a[i]-a[j]+18>=y:
                return 2
            if x>=y-b[i]-b[j] and y-b[i]-b[j]+18>=x:
                return 2
    return 3 
    
        

    
s=str(input())
print(ss(s))

C++ Code:

#include <iostream>
#include <set>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <ctime>
#include <fstream>
#include <vector>
#include <map>
#include <cmath>
using namespace std;

#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

int num[7];
int S = 0;
string s;
int Min = 10;

void back(int k, int op) {
    if (k == s.size()) {
        if (s[0] + s[1] + s[2] == s[3] + s[4] + s[5])
            Min = min(Min, op);
        return;
    }
    for (int j = '0'; j <= '9'; j++)
        if (s[k] == j)
            back(k + 1, op);
        else {
            char initial = s[k];
            s[k] = j;
            back(k + 1, op + 1);
            s[k] = initial;
        }
}

int main() {
    fast
    cin >> s;
    back(0, 0);
    cout << Min;
    return 0;
}


Comments

Submit
0 Comments
More Questions

292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number
1220B - Multiplication Table
1644A - Doors and Keys
1644B - Anti-Fibonacci Permutation
1610A - Anti Light's Cell Guessing
349B - Color the Fence
144A - Arrival of the General
1106A - Lunar New Year and Cross Counting
58A - Chat room
230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents